/**
 *
 */
package com.ssyt.examinationsystem.service.impl;

import com.ssyt.examinationsystem.dao.IRightInfoDAO;
import com.ssyt.examinationsystem.dao.impl.RightInfoDAOImpl;
import com.ssyt.examinationsystem.entity.RightInfo;
import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.service.IRightInfoService;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class RightInfoServiceImpl implements IRightInfoService {

    private IRightInfoDAO rightDao = new RightInfoDAOImpl();

    /**
     * 通过角色编号获取所有主菜单的集合的接口
     */
    public List<RightInfo> getMainMenuByRoleId(RoleInfo role) {
        // 调用DAO层的方法
        List<Map<String, Object>> list = rightDao.getMainMenuByRoleId(role);
        // 创建一个存放RightInfo的集合
        List<RightInfo> rightList = new ArrayList<RightInfo>();
        // 创建一个管理对象的句柄
        RightInfo right = null;
        if (CollectionUtils.isEmptyForList(list)) {
            for (Map<String, Object> map : list) {
                right = new RightInfo();
                right.setRightId(String.valueOf(map.get("rightId")));
                right.setRightName(String.valueOf(map.get("rightName")));
                right.setRightUrl((String.valueOf(map.get("rightUrl"))));
                right.setParentId((String.valueOf(map.get("parentId"))));
                rightList.add(right);
            }
        }

        return rightList;
    }

    /**
     * 通过角色编号获取所有子菜单的集合的接口
     */
    public List<RightInfo> getChildMenuByRoleId(RoleInfo role) {
        // 调用DAO层的方法
        List<Map<String, Object>> list = rightDao.getChildMenuByRoleId(role);
        // 创建一个存放RightInfo的集合
        List<RightInfo> rightList = new ArrayList<RightInfo>();
        // 创建一个管理对象的句柄
        RightInfo right = null;
        if (CollectionUtils.isEmptyForList(list)) {
            for (Map<String, Object> map : list) {
                right = new RightInfo();
                right.setRightId(String.valueOf(map.get("rightId")));
                right.setRightName(String.valueOf(map.get("rightName")));
                right.setRightUrl((String.valueOf(map.get("rightUrl"))));
                right.setParentId((String.valueOf(map.get("parentId"))));
                rightList.add(right);
            }
        }

        return rightList;
    }

    @Override
    public List<Map<String, Object>> getMainMenus() {

        return rightDao.getMainMenus();
    }

    @Override
    public List<Map<String, Object>> getChildMenus() {

        return rightDao.getChildMenus();
    }

    @Override
    public boolean grantRightByRole(String roleId, String ids) {
        // 设置返回值
        boolean flag = false;
        String[] idArray = ids.split(",");
        // 判断该角色是否有权限
        if (rightDao.isGrantByRoleId(roleId)) {
            // 有权限,删除权限
            if (rightDao.deleteGrantByRoleId(roleId)) {
                for (String rightId : idArray) {
                    // 设置角色权限
                    int row = rightDao.addGrantByRoleId(roleId, rightId);
                    if (row > 0) {
                        flag = true;
                    }
                }
            }
        } else {
            // 没有权限,设置权限
            for (String rightId : idArray) {
                // 设置角色权限
                int row = rightDao.addGrantByRoleId(roleId, rightId);
                if (row > 0) {
                    flag = true;
                }
            }
        }

        return flag;
    }

    @Override
    public List<Map<String, Object>> getSysMainMenus() {
        return rightDao.getSysMainMenus();
    }

    @Override
    public int addRightInfo(RightInfo right) {

        return rightDao.addRightInfo(right);
    }

    @Override
    public String getMaxRightIdByParentId(String parentId) {
        return rightDao.getMaxRightIdByParentId(parentId);
    }

}
